package me.linczh.bank.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import me.linczh.bank.bean.EmployeeBean;
import me.linczh.bank.bean.enums.Department;
import me.linczh.bank.bean.enums.Position;
import me.linczh.bank.dao.EmployeeDao;
import me.linczh.framework.persiste.IdGenerator;
import me.linczh.framework.query.Query;

public class DBEmployeeDao extends BaseDBDaoImpl implements EmployeeDao {

	@Override
	public EmployeeBean create(EmployeeBean _employee) {
		// TODO Auto-generated method stub
		String nextId = IdGenerator.nextId(_employee);
		if (nextId != null) {
			_employee.setEmployee(nextId);
			try {
				Statement s = super.conn.createStatement();
				s.execute("insert into employee_tbl values (" +
						"'" + _employee.getEmployee() + "'," +
						"'" + _employee.getUsername() + "'," +
						"'" + _employee.getPassword() + "'," +
						"'" + _employee.getDepartment() + "'," +
						"'" + _employee.getPosition() + "')");
				return _employee;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public boolean update(EmployeeBean _employee) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public EmployeeBean findById(String employeeID) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			ResultSet result = s.executeQuery("select * from employee_tbl where " +
					"employee='" + employeeID + "'");
			if (result.next()) {
				EmployeeBean bean = new EmployeeBean();
				bean.setEmployee(result.getString(1));
				bean.setUsername(result.getString(2));
				bean.setPassword(result.getString(3));
				bean.setDepartment(Department.valueOf(result.getString(4)));
				bean.setPosition(Position.valueOf(result.getString(5)));
				return bean;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<EmployeeBean> query(Query<EmployeeBean> query) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			ResultSet result = s.executeQuery(query.statement());
			List<EmployeeBean> list = new ArrayList<EmployeeBean>();
			while (result.next()) {
				EmployeeBean bean = new EmployeeBean();
				bean.setEmployee(result.getString(1));
				bean.setUsername(result.getString(2));
				bean.setPassword(result.getString(3));
				bean.setDepartment(Department.valueOf(result.getString(4)));
				bean.setPosition(Position.valueOf(result.getString(5)));
				list.add(bean);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return new ArrayList<EmployeeBean>();
	}

	@Override
	public boolean delete(EmployeeBean _employee) {
		// TODO Auto-generated method stub
		try {
			Statement s = super.conn.createStatement();
			int succ = s.executeUpdate("delete from employee_tbl where " +
					"employee='" + _employee.getEmployee() + "'");
			if (succ > 0) return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

}
